
var adController;
var adURL;
var t = null;
function createRandomNumber(varLength) {
var sourceArr = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
var randomKey;
var randomCode = "";

for (i=0; i<varLength; i++) {
randomKey = Math.floor(Math.random()*sourceArr.length);
randomCode = randomCode + sourceArr[randomKey];
}

/*
Possibly update some div as well?
document.getElementById('someDiv').innerHTML = randomCode;
*/

return randomCode;
}



function fetchAd(elementId){
	if (t==null)
		t = createRandomNumber(16);
		
	var posturl = 'http://r.admob.com/ad_source.php';
	var postdata ='rt=0&s=a14a8cc691d36bd&t='+t+'&f=html_no_js&y=text&'+
		 'k=Palm+Pre+social+card+game+mobile+phone+picture+cards+gambling+sports+free&'+
		 '&client_sdk=1&ex=1&v=20090331-ANDROID-cc0d740c1b8c3da5&'+
		 'u='+'Mozilla%2F5.0+(webOS%2F1.0%3B+U%3B+en-US)+AppleWebKit%2F525.27.1+(KHTML%2C+like+Gecko)+Version%2F1.0+Safari%2F525.27.1+Pre%2F1.0&i=';

	postdata='rt=0&s=a14a8cc691d36bd&f=html_no_js&y=text&client_sdk=1&ex=1'+
	'&v=20090331-ANDROID-cc0d740c1b8c3da5&k=Android+Palm+Pre+social+game+mobile+phone+cards+spades+gambling+picture+casual+sports+free';


	var element = document.getElementById(elementId);
	element.onclick = showAd;
	
	var myAjax = new Ajax.Request(posturl, {
		method: 'post',
		evalJSON: 'force',
		postBody: postdata,
		contentType: 'application/x-www-form-urlencoded',
		adElement: element,
		onComplete: function(transport){
				if (transport.status == 200) {
				
					var responseText = transport.responseText
				//	var response = responseText.evalJSON();
					var text = document.getElementById("ad_text");
					if (responseText.length>0)
						text.innerHTML = responseText;//response.text;
				//	adURL = response.url;
				
					
					var imgSrc = transport.getHeader('X-AdMob-Android-Category-Icon');
					
				
					if (imgSrc){
						var img = document.getElementById('adimg');
						img.src = imgSrc;
					}
					
				}
				else {
					setTimeout(polling_func, 90*1000);
				}
			
		},
		onFailure: function(transport){
			setTimeout(polling_func, 30*1000);
		}
	});
}

 var adTimeout = 0;
 var polling_func = function() {
 	
  	fetchAd('admob_ad');
	
 };


var adURL = "http://r.admob.com/ad_source.php?'rt=0&s=a14a8cc691d36bd&t='+t+'&f=html_no_js&y=text&'+ 'k=Palm+Pre+social+card+game+mobile+phone+picture+cards+gambling+sports+free&'+ '&client_sdk=1&ex=1&v=20090331-ANDROID-cc0d740c1b8c3da5&'+ 'u='+'Mozilla%2F5.0+(webOS%2F1.0%3B+U%3B+en-US)+AppleWebKit%2F525.27.1+(KHTML%2C+like+Gecko)+Version%2F1.0+Safari%2F525.27.1+Pre%2F1.0&i=";

function showAd(){
	
		var myAjax = new Ajax.Request(adURL, {
		method: 'get',
		
		contentType: 'application/x-www-form-urlencoded',
		adElement: element,
		onComplete: function(transport){
				if (transport.status == 302) {
				
					var location = transport.getHeader('Location');
					
					 adController.controller.serviceRequest("palm://com.palm.applicationManager", {
	  method: "open",
	  parameters:  {
	      id: 'com.palm.app.browser',
	      params: {
	          target: location
	      }
	  }
	});
				}
				else {
				
				}
			
		},
		onFailure: function(transport){
			element.innerHTML = transport.responseText;
		}
	});
	
	
 
}

